using Godot;
using System;
using CurrentGame.Utils.Interpolation;

namespace CurrentGame.Placements;

[Tool]
public partial class PlacableTexts : Node2D
{
	[Export(PropertyHint.MultilineText)]
	public string Text
	{
		get => _text;
		set
		{
			_text = value;
			if (_labelRef != null) _labelRef.Text = value;
		}
	}
	private string _text;

	[Export]
	public bool ShowText
	{
		get => _showText;
		set
		{
			_showText = value;
			_interpolation.Value = value ? 1.0f : 0.0f;
		}
	}
	private bool _showText = true;

	private LerpInterpolation _interpolation;
	private Label _labelRef;
	private bool _editorHint;


	public PlacableTexts()
	{
		_interpolation = new LerpInterpolation(0.016f, new Vector3(0.001f, -1f, -1f))
		{
			Weight = 0.05f,
		};
	}

	public override void _Ready()
	{
		_editorHint = Engine.IsEditorHint();
		_labelRef = GetNode<Label>("Label");

		_interpolation.Value = _showText ? 1.0f : 0.0f;
		_interpolation.Teleport();

		_labelRef.Text = _text;
		var current = _interpolation.Value;
		if (_editorHint) current = 1.0f;
		_labelRef.Modulate = new Color(1.0f, 1.0f, 1.0f, current);
	}

	public override void _Process(double delta)
	{
		if (!_editorHint)
		{
			if (_interpolation.Process(delta))
			{
				_labelRef.Modulate = new Color(1.0f, 1.0f, 1.0f, _interpolation.Value);
			}
		}
	}
}
